<?php declare(strict_types=1);
/**
 * @author      guosir
 * @copyright   2023-2025 owner
 **/

namespace Srv\Apps\Apis\Banner;

use Srv\Apps\Apis\Banner\Ctl\BannerCtl;
use Srv\Apps\Apis\Log\Ctl\LogCtl;
use Srv\Apps\Proto\DTO\Banner\BannerAddDTO;
use Srv\Apps\Proto\DTO\Banner\BannerEditDTO;
use Srv\Apps\Proto\DTO\Banner\BannerListDTO;
use Srv\Apps\Proto\Enum\ErrCodeEnum;
use Srv\Apps\Proto\Enum\LangKeyEnum;
use Srv\Apps\Proto\Enum\Log\LogResultEnum;
use Srv\Apps\Proto\VO\Banner\BannerListVO;
use Srv\Libs\ConfModel\RouteConfData;
use Srv\Libs\Frame\ActionAbstract;
use Srv\Libs\Frame\DTOAbstract;
use Srv\Libs\Frame\Lang;
use Srv\Libs\Frame\Page;
use Srv\Libs\Frame\ResponseData;
use Srv\Libs\Frame\VOAbstract;

final class BannerAct extends ActionAbstract
{
    /**
     * @url /app/banner/list
     * @auth bannerList
     * @req Srv.Apps.Proto.DTO.Banner.BannerListDTO
     * @rsp Srv.Apps.Proto.VO.Banner.BannerListVO
     * Banner列表
     */
    public function bannerList(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        if(!($DTOClass instanceof BannerListDTO) || !($VOClass instanceof BannerListVO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $Page                   = new Page();
        $currPage               = max(1, $DTOClass->getCurrPage());
        $pageSize               = $DTOClass->getPageSize() < 1 ? 12 : $DTOClass->getPageSize();
        $Page->init(0, $pageSize, $currPage);
        $whereData              = $RouteConfData->getParamsResData($DTOClass);
        $VOClass                = BannerCtl::tag()->getBannerList($Page, $whereData);
        return ResponseData::rspSuccAuto('', $VOClass, []);
    }

    /**
     * @url /app/banner/add
     * @auth bannerAdd
     * @req Srv.Apps.Proto.DTO.Banner.BannerAddDTO
     * Banner新增
     */
    public function bannerAdd(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        unset($VOClass);
        if(!($DTOClass instanceof BannerAddDTO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $setData    = $RouteConfData->getParamsResData($DTOClass);
        $LogCtl     = LogCtl::tag();
        $logData    = $setData;
        $banrId     = BannerCtl::tag()->bannerAdd($setData);
        if($banrId < 1){
            $errMsg = 'Banner新增失败';
            $LogCtl->logAdd(LogCtl::BANNER_ADD, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }
        $errMsg     = 'Banner新增成功';
        $LogCtl->logAdd(LogCtl::BANNER_ADD, LogResultEnum::LOG_RESULT_SUCCESS, $errMsg, $logData);
        return ResponseData::rspSuccAuto();
    }

    /**
     * @url /app/banner/edit
     * @auth bannerEdit
     * @req Srv.Apps.Proto.DTO.Banner.BannerEditDTO
     * Banner编辑
     */
    public function bannerEdit(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        unset($VOClass);
        if(!($DTOClass instanceof BannerEditDTO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $banrId         = intval($DTOClass->getBanrId());
        $setData        = $RouteConfData->getParamsResData($DTOClass);
        $BannerCtl      = BannerCtl::tag();
        $LogCtl         = LogCtl::tag();
        $bannerSrc      = $BannerCtl->getBannerSrc($banrId);
        if(!isset($bannerSrc['banr_id'])){
            $errMsg     = 'Banner不存在';
            $logData    = $setData;
            $LogCtl->logAdd(LogCtl::BANNER_EDIT, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }
        $result         = $BannerCtl->bannerEdit($banrId, $setData);
        $logData        = ['new' => $setData, 'old' => $bannerSrc];
        if(!$result){
            $errMsg     = 'Banner编辑失败';
            $LogCtl->logAdd(LogCtl::BANNER_EDIT, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }
        $errMsg         = 'Banner编辑成功';
        $LogCtl->logAdd(LogCtl::BANNER_EDIT, LogResultEnum::LOG_RESULT_SUCCESS, $errMsg, $logData);
        return ResponseData::rspSuccAuto();
    }
}